package Demo1;

public class T1 {
    /*
    * 模拟电影院 100张票 三个窗口 (多线程)
    */
    public static void main(String[] args) {
        TicketTask task = new TicketTask();
        Thread t1= new Thread(task,"A:");
        Thread t2= new Thread(task,"B:");
        Thread t3= new Thread(task,"C:");
        t1.start();
        t2.start();
        t3.start();
    }
}
class TicketTask implements Runnable{
    private int ticket = 1000;
    @Override
    public void run() {
        while(true){
            synchronized (TicketTask.class){
                //给核心代码块上锁   需要保证 多条线程使用的是同一把锁
                if (ticket == 0){
                    break;
                }
                System.out.println(Thread.currentThread().getName()+"卖出了第"+ticket+"张票");
                ticket--;
            }
        }
    }
}
